<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Process{
	
	private $_CI;
	
	public static $basePriceImpression = 500;
	public static $basePriceClick = 2000;
	
	function __construct(){
		$this->_CI =  &get_instance();
		
		$this->pieColours = array("#FF0000","#357EC7","#8D38C9","#4CC417","#F87431","#C25A7C","#B4BA22","#8DCDC1","#747E80","#CDCDCD");
	}
	
	function getAffiliate($pubId){
		$this->_CI->load->model("statistic_model");
		return $this->_CI->statistic_model->get_affiliate($pubId);
	}
	function getLocationStat($startDate=false,$endDate=false,$from=0,$limit=30){
		
		$this->_CI->load->model("statistic_model");
		
		
		$response = $this->_CI->statistic_model->get_locations($startDate->format("Y-m-d 00:00:00"),$endDate->format("Y-m-d 23:59:59"),$from,$limit);

		return $response;
	}
	
	function getLocationStatsTotal($startDate=false,$endDate=false){
		$this->_CI->load->model("statistic_model");
		
		$response	= $this->_CI->statistic_model->get_locations_total($startDate->format("Y-m-d 00:00:00"),$endDate->format("Y-m-d 23:59:59"));
		
		return $response;
	}
	
	function getLocationDailyStats($pubId,$startDate=false,$endDate=false,$from=0,$limit=30){
		$this->_CI->load->model("statistic_model");
			
		$response = array();
		$response = $this->_CI->statistic_model->get_location_daily($pubId,$startDate->format("Y-m-d 00:00:00"),$endDate->format("Y-m-d 23:59:59"),$from,$limit);
		
		return $response;
	}
	
	//get total list of statistic
	function getLocationDailyStatsTotal($pubId,$startDate=false,$endDate=false){
		$this->_CI->load->model("statistic_model");
		
		$response	= $this->_CI->statistic_model->get_location_daily_total($pubId,$startDate->format("Y-m-d 00:00:00"),$endDate->format("Y-m-d 23:59:59"));
		
		return $response;
	}
	
	function getBuildingLocation($from=0,$limit=30){
		$this->_CI->load->model("hotspot_model");
		
		$response	= $this->_CI->hotspot_model->get_building_location($from,$limit);
		
		return $response;
	}
	//get total list of building
	function getTotalBuilding(){
		$this->_CI->load->model("hotspot_model");
		
		return $this->_CI->hotspot_model->get_total_building();
	}
	
	//get total aktif user in a building
	function getTotalAktifUserBuilding($buildingid,$startDate,$endDate){
		$this->_CI->load->model("hotspot_model");
		
		return $this->_CI->hotspot_model->get_total_aktif_user_building($buildingid,$startDate,$endDate);
	}
	//get total registered user in a building
	function getTotalRegisteredUserBuilding($buildingid,$startDate,$endDate){
		$this->_CI->load->model("hotspot_model");
		
		return $this->_CI->hotspot_model->get_total_registered_user_building($buildingid,$startDate,$endDate);
	}
	
	//get list of user details
	function getUsers($gender=false,$age=false,$expense=false,$startDate=false,$endDate=false,$from,$limit){
		$this->_CI->load->model("user_model");

		return $this->_CI->user_model->get_users($gender,$age,$expense,$startDate,$endDate,$from,$limit);
	}

	function getMaleFemalePieChart(){
		$this->_CI->load->model("dashboard_model");
		
		$data =  $this->_CI->dashboard_model->get_users_gender();
		
		$title = new title("Gender");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		
		$i=0;
		$pieval = array();
		foreach($data as $row){
			$pieval[$i]=new pie_value(intval($row["total"]),$row["gender"]=="m" ? "Male" : "Female");
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b> (#val# users)');
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
	}
	function getAgeRangePieChart(){
		$this->_CI->load->model("dashboard_model");
		
		$data =  $this->_CI->dashboard_model->get_age_range();
		
		$title = new title("Age range");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		
		$i=0;
		$pieval = array();
		foreach($data as $k=>$v){
			$pieval[$i]=new pie_value(intval($v),$k);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# yr <br> <b>#percent#</b> (#val# users)');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
	}
	//get registration statistics by date
	function getRegistrationStatistic(){
		$this->_CI->load->model("dashboard_model");
		
		return $this->_CI->dashboard_model->get_registration_statistic();
	}
	//get active users statistics by date
	function getActiveUser(){
		$this->_CI->load->model("dashboard_model");
		
		return $this->_CI->dashboard_model->get_active_statistics();
	}
	function getLineStatistic1(){
		$this->_CI->load->model("dashboard_model");
		
		$stats = $this->_CI->dashboard_model->get_line_statistic1();
		
		$hNum = 0;
		
		$chart = new open_flash_chart();
		$chart->set_title(new title('Daily Hotspot Usage'));
		
		
		for($i=0;$i<sizeof($stats);$i++){
			
			$d = new solid_dot();
			$d->size(5)->halo_size(1)->colour($stats[$i]["colour"]);
			$d->tooltip($stats[$i]["title"]."<br>#date:d M Y#"."<br>#val#");
			
			$x_data = $stats[$i]["x_data"];
			$y_data = $stats[$i]["y_data"];
			
			$scatter_values = array();
			//read through data
			for($x=0;$x<sizeof($x_data);$x++){
				$scatter_values[] = new scatter_value(strtotime($x_data[$x]),$y_data[$x]);
			}
			
			$line = new scatter_line($stats[$i]["colour"],"2");
			$line->set_values($scatter_values);
			$line->set_default_dot_style($d);
			
			$chart->add_element($line);
			
			if($stats[$i]["hNum"]>$hNum)
				$hNum = $stats[$i]["hNum"];
		}
		
		//set tooltips for x-axis 
		$t = new tooltip('');
		$t->set_shadow( false );
		$t->set_stroke(3);
		$t->set_colour( "#A2ACBA" );
		$t->set_background_colour( "#ffffff" );
		$t->set_title_style( "{font-size: 14px;font-weight: bold; color: #555555;}" );
		$t->set_body_style( "{font-size: 10px; color: #555555;}" );

		
		
		//prepare x-axis label
		$x_labels = new x_axis_labels();
		$x_labels->text('#date:d/m#');
		$x_labels->visible_steps(1);
		$x_labels->set_steps(86400);
		
		//prepare x-axis line
		$dates = $stats[0]["x_data"];
		$x = new x_axis();
		$x->set_range(strtotime($dates[0]),strtotime($dates[sizeof($dates)-1]));
		$x->set_steps(86400);
		$x->set_labels($x_labels);
		$x->set_grid_colour("#e5e5e5");
		$x->colour("#A2ACBA");
		$x->tick_height(0);
		$x->stroke(3);

		
		//calculate range data for y axis
		$digit_length = strlen(strval($hNum)) - 1;
		$decimaler = pow(10,$digit_length);
		if($hNum < (4*$decimaler)) $decimaler /= 10;
		
		$ranges = round($hNum/(4*$decimaler)) * $decimaler;
		$y = new y_axis();
		$y->set_range( 0,round($hNum),$ranges);
		$y->set_grid_colour("#e5e5e5");
		$y->set_colour("#A2ACBA");
		$y->set_stroke(3);
		$y->set_tick_length(0);
		
		$chart->set_y_axis( $y );
		$chart->set_x_axis( $x );
		$chart->set_bg_colour('#e6f2fa');
		//$chart->set_tooltip($t);
		return $chart->toPrettyString();
		
	}

	function getTopUsedBrowser(){
		$this->_CI->load->model("dashboard_model");
		
		if(!($data = $this->_CI->dashboard_model->get_top_used_browser())) return false;
		
		$title = new title("Most used browser");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		
		$i=0;
		$pieval = array();
		foreach($data as $k=>$v){
			$pieval[$i]=new pie_value(intval($v),$k);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
			if($i==6) break;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b>');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
		
	}

	function getTopUsedPlatForm(){
		$this->_CI->load->model("dashboard_model");
		
		if(!($data = $this->_CI->dashboard_model->get_top_used_platform())) return false;
		
		$title = new title("Most used platform");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		
		$i=0;
		$pieval = array();
		foreach($data as $row){
			$pieval[$i]=new pie_value(intval($row["total"]),$row["os"]);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
			if($i==8) break;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b> ');
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();
		
	}
	function getUsersInterestsPieChart(){
		$this->_CI->load->model("dashboard_model");
		
		$data =  $this->_CI->dashboard_model->get_users_interests();
		
		$title = new title("Users Interests");
		$chart = new open_flash_chart();
		$chart->set_title($title);
		$chart->set_bg_colour('#e6f2fa');
		
		$i=0;
		$pieval = array();
		foreach($data as $val){
		
			$pieval[$i]=new pie_value(intval($val["jumlah"]),$val["eng"]);
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$i++;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		$pie->set_tooltip( '#label# <br> <b>#percent#</b> (#val# users)');
		
		
		$chart->add_element( $pie );
		
		return $chart->toPrettyString();	
	}
	function getTopLocationsBar(){
		$this->_CI->load->model("dashboard_model");
		
		$data =  $this->_CI->dashboard_model->get_top_locations();
		
		$hNum=0;
		$barVal=array();
		$xLabel=array();
		
		$i=0;
		foreach($data as $val):
		// make the last bar a different colour:
		$bar = new bar_value((int)$val["total"]);
		$bar->set_colour( $this->pieColours[$i] );
		$bar->set_tooltip( $val["building"].'<br>#val# ' );
		$barVal[] = $bar;
		if($hNum < (int)$val["total"]) $hNum=(int)$val["total"];		
		
		$xLabel[]= new x_axis_label("",$this->pieColours[$i] ,2,0);		
		$i++;
		endforeach;
		$title = new title(" ");

		$bar = new bar_filled();
		$bar->set_values( $barVal );
		$bar->set_key('',2);
		$bar->colour = '#D54C78';
		
		$x_labels = new x_axis_labels();
		$x_labels->text('');
		$x_labels->visible_steps(1);
		$x_labels->set_steps(86400);
		
		$x_axis = new x_axis();
		$x_axis->colour = '#909090';
		$x_axis->set_labels( $x_labels );
		$x_axis->set_grid_colour("#e5e5e5");
		$x_axis->set_colour("#A2ACBA");
		$x_axis->set_stroke(3);
		$x_axis->set_tick_height(0);
		//calculate range data for y axis
		$digit_length = strlen(strval($hNum)) - 1;
		$decimaler = pow(10,$digit_length);
		if($hNum < (4*$decimaler)) $decimaler /= 10;
		
		$ranges = round($hNum/(4*$decimaler)) * $decimaler;
		$y = new y_axis();
		$y->set_range( 0,round($hNum),$ranges);
		$y->set_grid_colour("#e5e5e5");
		$y->set_colour("#A2ACBA");
		$y->set_stroke(3);
		$y->set_tick_length(0);
		
		$chart = new open_flash_chart();
		$chart->set_bg_colour('#e6f2fa');
		$chart->set_title( $title );
		$chart->add_element( $bar );
		$chart->set_x_axis($x_axis);
		$chart->set_y_axis($y);
		return $chart->toPrettyString();	
	}
	function getTopLocationsPieChart(){
		$this->_CI->load->model("dashboard_model");
		
		$data =  $this->_CI->dashboard_model->get_top_locations();
		
		$i=0;
		$pieval = array();
		foreach($data as $val){
		
			$pieval[$i]=new pie_value(intval($val["total"]),'');
			$pieval[$i]->set_colour($this->pieColours[$i]);
			$pieval[$i]->set_tooltip( $val["location"].'<br>'.$val["building"].'<br>#val#(#percent#)');
			$i++;
		}
		
		$pie = new pie();
		$pie->set_values($pieval);
		
		
		$title = new title("Most Active Locations");
		$chart = new open_flash_chart();
		$chart->set_bg_colour('#e6f2fa');
		$chart->set_title( $title );
		$chart->add_element( $pie );
		return $chart->toPrettyString();	
	}
}